package com.aube.base.goods;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aube.base.IPluginBaseListener;
import com.aube.base.controller.DataManager;
import com.aube.interact.base.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.video.model.GoodsItem;
import com.huyn.baseframework.dynamicload.video.model.GoodsModel;
import com.huyn.baseframework.utils.Utils;

/**
 * Created by hill on 16/11/3.
 */

public class GoodsCartItemView extends LinearLayout {
    private String TAG = "Aube/CartItemView";
    private Context mContext;

    private GoodsHotpotLayout.IAddFavorListener mAddFavorListener;
    private IPluginBaseListener mBaseListener;

    private RelativeLayout mParentGoodsStoreLayout;
    private GoodsDetailLayout mGoodsDetailLayout;
    private LinearLayout mRoot;

    private GoodsItem mGoodsItem;
    private GoodsModel mGoodModel;

    private TextView mPurchaseText;
    private TextView mCartitemTitle, mCartitemPrice, mCartitemTimeTrack;
    private ImageView mCartitemImage;

    private boolean mBSelected = false;
    private String mTimeTrackPrefix = "出现于";
    private String mTimeTrackSuffix = " >";

    public GoodsCartItemView(Context context) {
        this(context, null);
    }

    public GoodsCartItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GoodsCartItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        mRoot = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.aube_intera_layout_cartitem, this);

        mCartitemTitle = (TextView) findViewById(R.id.cartitem_title);
        mCartitemPrice = (TextView) findViewById(R.id.cartitem_price);
        mCartitemTimeTrack = (TextView) findViewById(R.id.cartitem_timetrack);
        mCartitemImage = (ImageView) findViewById(R.id.cartitem_img);
        mCartitemImage.setScaleType(ImageView.ScaleType.CENTER_CROP);

        mPurchaseText = (TextView) findViewById(R.id.cartitem_purchase);
        mPurchaseText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mBSelected) {
                    mBSelected = false;
                    mPurchaseText.setBackgroundResource(R.drawable.bg_cartitem_purchase);
                    mPurchaseText.setText(mGoodModel.getBeforeDesc());
                    mPurchaseText.setTextColor(mContext.getResources().getColor(R.color.text_purchase));
                    DataManager.getInstance(mContext).delete(mGoodModel.getId());
                    mBaseListener.removeFavorGoods(mGoodModel.getId());
                } else {
                    mBSelected = true;
                    mPurchaseText.setBackgroundResource(R.drawable.bg_cartitem_purchased);
                    mPurchaseText.setText(mGoodModel.getAfterDesc());
                    mPurchaseText.setTextColor(mContext.getResources().getColor(R.color.text_purchased));
                    DataManager.getInstance(mContext).update(mGoodModel.getId(), mGoodsItem);
                    mBaseListener.addFavorGoods(mGoodsItem);
                }
            }
        });

        mCartitemImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1: fill data
                mGoodsDetailLayout.fillData(mGoodModel, mAddFavorListener);
                mGoodsDetailLayout.setupCallback(mOnCartInfoUpdate);

                // 2: set up view & animate it
                int[] location = new int[2];
                mCartitemImage.getLocationInWindow(location);
                int width = mCartitemImage.getWidth();
                int height = mCartitemImage.getHeight();
                int[] args = new int[4];
                args[0] = location[0];
                args[1] = location[1];
                args[2] = width;
                args[3] = height;
                mGoodsDetailLayout.startAnim(args, new GoodsDetailLayout.IAnimEndListener() {
                    @Override
                    public void onOveralAnimEnd() {
                    }

                    @Override
                    public void onStartReverseAnim() {
                        // alpha in original view
                        ObjectAnimator animator = ObjectAnimator.ofFloat(mParentGoodsStoreLayout, "alpha", 0.0f, 1.0f);
                        animator.setDuration(400);
                        animator.setTarget(mParentGoodsStoreLayout);
                        animator.start();
                    }
                });

                // 3: alpha out original view
                ObjectAnimator animator = ObjectAnimator.ofFloat(mParentGoodsStoreLayout, "alpha", 1.0f, 0.0f);
                animator.setDuration(400);
                animator.setTarget(mParentGoodsStoreLayout);
                animator.start();
            }
        });

        mCartitemTimeTrack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mParentGoodsStoreLayout.setVisibility(GONE);
                mBaseListener.onProgressSeekEvent(mGoodModel.startSeconds);
            }
        });
    }

    public void bindGoodsDetailLayout(GoodsDetailLayout layout) {
        mGoodsDetailLayout= layout;
    }

    public void setupCallback(GoodsHotpotLayout.IAddFavorListener listener, IPluginBaseListener baseListener) {
        mAddFavorListener = listener;
        mBaseListener = baseListener;
    }

    public void fillData(GoodsModel data, RelativeLayout layout) {
        if(data == null) return;

        mParentGoodsStoreLayout = layout;
        mGoodModel = data;

        ImageLoader.getInstance(getContext()).fillImage(mCartitemImage, data.getGoodsShowPicUrl(), Utils.dip2px(mContext, 150), Utils.dip2px(mContext, 150));

        if(data.goodsVo != null) {
            fillInternalData(data.goodsVo);
            mCartitemTimeTrack.setText(mTimeTrackPrefix + transferSecstoMins(data.startSeconds) + mTimeTrackSuffix);
        } else {
            Utils.Log(Utils.LogType.DEBUG, TAG, "GoodsModel: goodsidList size != 1");
        }
    }

    private void fillInternalData(GoodsItem data) {
        mGoodsItem = data;

        mCartitemTitle.setText(data.tlTitle);
        CharSequence charSequence = Html.fromHtml(data.parsePrice(data.getPriceIndex()));
        mCartitemPrice.setText(charSequence);
        mCartitemPrice.setMovementMethod(LinkMovementMethod.getInstance());

        if(DataManager.getInstance(mContext).hasCollected(data._id)) {
            mBSelected = true;
            mPurchaseText.setBackgroundResource(R.drawable.bg_cartitem_purchased);
            mPurchaseText.setText(data.afterDesc);
            mPurchaseText.setTextColor(mContext.getResources().getColor(R.color.text_purchased));
        } else {
            mBSelected = false;
            mPurchaseText.setBackgroundResource(R.drawable.bg_cartitem_purchase);
            mPurchaseText.setText(data.beforeDesc);
            mPurchaseText.setTextColor(mContext.getResources().getColor(R.color.text_purchase));
        }
    }

    private String transferSecstoMins(int value) {
        int min = value / 60;
        int sec = value % 60;
        String str = String.format("%02d:%02d", min, sec);
        return str;
    }

    public OnCartInfoUpdate mOnCartInfoUpdate = new OnCartInfoUpdate() {
        @Override
        public void onPurchaseUpdateEvent(boolean bPurchase) {
            if(bPurchase) {
                mBSelected = true;
                mPurchaseText.setBackgroundResource(R.drawable.bg_cartitem_purchased);
                mPurchaseText.setText(mGoodModel.getAfterDesc());
                mPurchaseText.setTextColor(mContext.getResources().getColor(R.color.text_purchased));
            } else {
                mBSelected = false;
                mPurchaseText.setBackgroundResource(R.drawable.bg_cartitem_purchase);
                mPurchaseText.setText(mGoodModel.getBeforeDesc());
                mPurchaseText.setTextColor(mContext.getResources().getColor(R.color.text_purchase));
            }
        }
    };

    public interface OnCartInfoUpdate {
        public void onPurchaseUpdateEvent(boolean bPurchase);
    }
}
